package cn.ultrarisk.scadmin.manager.scheduler;

import cn.ultrarisk.scadmin.manager.biz.common.dao.DictDAO;
import cn.ultrarisk.scadmin.manager.biz.common.entity.Dict;
import cn.ultrarisk.scadmin.manager.biz.common.service.OperationLogService;
import cn.ultrarisk.scadmin.manager.biz.monitor.service.*;
import cn.ultrarisk.scadmin.manager.biz.solr.service.*;
import cn.ultrarisk.scadmin.manager.biz.zookeeper.service.ZkBackupService;
import cn.ultrarisk.scadmin.manager.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * ExpiredScheduler
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/5/17
 * @since V1.0
 */
@Component
public class ExpiredScheduler {
    @Autowired
    private SolrNodeService solrNodeService;
    @Autowired
    private SolrCollectionService solrCollectionService;
    @Autowired
    private SolrShardService solrShardService;
    @Autowired
    private SolrReplicaService solrReplicaService;
    @Autowired
    private SolrCacheService solrCacheService;
    @Autowired
    private SolrNodeMonitorService solrNodeMonitorService;
    @Autowired
    private SolrReplicaMonitorService solrReplicaMonitorService;
    @Autowired
    private SolrCacheMonitorService solrCacheMonitorService;
    @Autowired
    private ZkBackupService zkBackupService;
    @Autowired
    private DictDAO dictDAO;
    @Autowired
    private ErrorLogService errorLogService;
    @Autowired
    private SlowLogService slowLogService;
    @Autowired
    private OperationLogService operationLogService;

    /**
     * 清除过期的solr采集数据
     *
     * @throws Exception
     */
    public void execute() throws Exception {
        Integer collectionExpiredDay = dictDAO.getIntValue(Dict.SOLR_COLLECTION_EXPIRED_DAY);
        Integer nodeExpiredDay = dictDAO.getIntValue(Dict.SOLR_NODE_EXPIRED_DAY);
        Integer monitorExpiredHour = dictDAO.getIntValue(Dict.SOLR_MONITOR_EXPIRED_HOUR);
        Integer zkBackupExpiredDay = dictDAO.getIntValue(Dict.ZK_BACKUP_EXPIRED_DAY);
        Integer errorLogExpiredDay = dictDAO.getIntValue(Dict.ERROR_LOG_EXPIRED_DAY);
        Integer slowLogExpiredDay = dictDAO.getIntValue(Dict.SLOW_LOG_EXPIRED_DAY);
        Integer operateLogExpiredDay = dictDAO.getIntValue(Dict.OPERATE_LOG_EXPIRED_DAY);
        Integer schedulerLogExpiredDay = dictDAO.getIntValue(Dict.SCHEDULER_LOG_EXPIRED_DAY);

        long collectionExpiredTime = DateUtil.addDay(System.currentTimeMillis(), -1 * collectionExpiredDay);
        long nodeExpiredTime = DateUtil.addDay(System.currentTimeMillis(), -1 * nodeExpiredDay);
        long monitorExpiredTime = DateUtil.addHour(System.currentTimeMillis(), -1 * monitorExpiredHour);
        long zkBackupExpiredTime = DateUtil.addDay(System.currentTimeMillis(), -1 * zkBackupExpiredDay);
        long errorLogExpiredTime = DateUtil.addDay(System.currentTimeMillis(), -1 * errorLogExpiredDay);
        long slowLogExpiredTime = DateUtil.addDay(System.currentTimeMillis(), -1 * slowLogExpiredDay);
        long operateLogExpiredTime = DateUtil.addDay(System.currentTimeMillis(), -1 * operateLogExpiredDay);
        long schedulerLogExpiredTime = DateUtil.addDay(System.currentTimeMillis(), -1 * schedulerLogExpiredDay);

        solrCollectionService.clearExpiredData(collectionExpiredTime);
        solrShardService.clearExpiredData(collectionExpiredTime);
        solrReplicaService.clearExpiredData(collectionExpiredTime);
        solrCacheService.clearExpiredData(collectionExpiredTime);

        solrNodeService.clearExpiredData(nodeExpiredTime);

        solrNodeMonitorService.clearExpiredData(monitorExpiredTime);
        solrReplicaMonitorService.clearExpiredData(monitorExpiredTime);
        solrCacheMonitorService.clearExpiredData(monitorExpiredTime);

        zkBackupService.clearExpiredData(zkBackupExpiredTime);
        errorLogService.clearExpiredData(errorLogExpiredTime);
        slowLogService.clearExpiredData(slowLogExpiredTime);
        operationLogService.clearExpiredOperateData(operateLogExpiredTime);
        operationLogService.clearExpiredSchedulerData(schedulerLogExpiredTime);

        operationLogService.createExpiredDataLog();
    }
}
